package com.tulingxueyuan.order.service.impl;

import com.tulingxueyuan.order.mapper.OrderMapper;
import com.tulingxueyuan.order.pojo.Order;
import com.tulingxueyuan.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
 * @Author li
 * @Date 2025/4/26 17:28
 * @description: TODO
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    @Qualifier(value = "orderRestTemplate")
    private RestTemplate restTemplate;


    @Transactional
    @Override
    public Order create(Order order) {
        // 能否插入成功？
        orderMapper.insert(order);

        // 扣减库存，能否成功？
        MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
        paramMap.add("productId", order.getProductId());
        String msg = restTemplate.postForObject("http://localhost:8071/stock/reduce", paramMap, String.class);
        // 异常
        int a = 1 / 0;
        return order;
    }
}
